﻿using Model;
using Business.IFace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MB.Orm.DB;
using Public.DataAccess;
using MB.RuleBase.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Business
{
    public class BLocation
    {
        public List<CellNo> GetCellNoList()
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<CellNo> CellNolist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<CellNo>("Location", "GetCellNolist", "");
                return CellNolist;
            }
        }
        public int AddLocation(Location location)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<Location>
                            ("Location", "AddLocation", location);
        }
        public int EditLocationByID(Location location)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<Location>
                            ("Location", "EditLocationByID", location);
        }
        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="IsSetAll">加全部选项 O表示加，1为不加</param>
        /// <returns></returns>
        public List<Warehouse> GetWarehouseList(int IsSetAll)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Warehouse> Warehouselist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Warehouse>("Location", "GetWarehouseInfo", "");
                if (IsSetAll <= 0)
                {
                    Warehouse w = new Warehouse();
                    w.WAREHOUSECODE = "";
                    w.WAREHOUSENAME = "--全部--";
                    w.SHOPNAME = "--全部--";
                    w.WAREHOUSEID = 0;
                    Warehouselist.Add(w);
                }
                return Warehouselist;
            }
        }

        public List<Location> GetLocationCodeList(int IsSetAll)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Location> lstLocation = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Location>("Location", "GetLocationCodeList", "");
                if (IsSetAll <= 0)
                {
                    Location _Location = new Location();
                    _Location.LOCATIONCODE = "--全部--";
                    _Location.LOCATIONID = 0;
                    lstLocation.Add(_Location);
                }
                return lstLocation;
            }
        }

        public List<Goods> GetGoodsCodeList(int IsSetAll, int IsSetNone = 1, string Beginwith = "")
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Goods> lstGoods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("LocationGoods", "GetGoodsCodeList", "");
                if (!string.IsNullOrEmpty(Beginwith))
                {
                    if (Beginwith == "other")
                    {
                        lstGoods = lstGoods.Where(_ => !_.GOODSCODE.StartsWith("0")
                            && !_.GOODSCODE.StartsWith("2")
                            && !_.GOODSCODE.StartsWith("3")
                            && !_.GOODSCODE.StartsWith("WOC")).ToList();
                    }
                    else
                    {
                        lstGoods = lstGoods.Where(_ => _.GOODSCODE.StartsWith(Beginwith)).ToList();
                    }
                }
                if (IsSetAll <= 0)
                {
                    Goods _Goods = new Goods();
                    _Goods.GOODSCODE = "--全部--";
                    _Goods.GOODSNAME = "--全部--";
                    _Goods.GOODSID = 0;
                    lstGoods.Add(_Goods);
                }
                if (IsSetNone <= 0)
                {
                    Goods _Goods = new Goods();
                    _Goods.GOODSCODE = "----";
                    _Goods.GOODSNAME = "----";
                    _Goods.GOODSID = 0;
                    lstGoods.Add(_Goods);
                }
                return lstGoods;
            }
        }

        public int DeleteLocation(int LocationID)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("Location", "DeleteLocation", LocationID);
        }

        /// <summary>
        /// 根据LocationCode获取货位
        /// </summary>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        public Location CheckLocation(string locationCode, string LOCATIONID = "")
        {
            LOCATIONID = string.IsNullOrEmpty(LOCATIONID) ? "0" : LOCATIONID;
            Location Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Location>("Location", "CheckLocation", locationCode, LOCATIONID).FirstOrDefault();
            return Info;
        }
        //查询货位资料列表
        public List<Location> GetLocationList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<Location> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<Location>
                                                ("Location", "GetLocationInfo", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }

        public List<LocationGoods> GetLocationGoodsList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<LocationGoods> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<LocationGoods>
                                                ("LocationGoods", "GetLocationGoodsInfo", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }

        public Goods CheckGoods(string goodsCode)
        {
            Goods Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("LocationGoods", "CheckGoods", goodsCode).FirstOrDefault();
            return Info;
        }

        public int DeleteLocationGoods(int LocationID)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("LocationGoods", "DeleteLocationGoods", LocationID);
        }

        public int AddLocationGoods(LocationGoods locationgoods)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<LocationGoods>
                            ("LocationGoods", "AddLocationGoods", locationgoods);
        }
        public int EditLocationGoodsByID(LocationGoods locationgoods)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<LocationGoods>
                            ("LocationGoods", "EditLocationGoodsByID", locationgoods);
        }

        public Goods CheckGoodsByBarCode(string barCode)
        {
            Goods Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<Goods>("LocationGoods", "CheckGoodsByBarCode", barCode).FirstOrDefault();
            return Info;
        }
        public List<LocationGoods> CheckLocationGoodsPrimary(int LOCATIONID, int GOODSID, string LOCATIONGOODSID)
        {
            LOCATIONGOODSID = string.IsNullOrEmpty(LOCATIONGOODSID) ? "0" : LOCATIONGOODSID;
            List<LocationGoods> Info = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LocationGoods>("LocationGoods", "CheckLocationGoodsPrimary"
                , LOCATIONID, GOODSID, int.Parse(LOCATIONGOODSID));
            return Info;
        }


        public List<LocationInventoryInfo> GetLocationInventoryList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<LocationInventoryInfo> list = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<LocationInventoryInfo>
                                                ("Location", "GetLocationInventoryList", pageIndex, pageSize, out total, parValues);
                return list;
            }
        }
    }
}
